package android.slc.user.ui.ud.vmbox

import android.slc.appbase.api.slc.callback.SlcObserver
import android.slc.appbase.api.slc.callback.po.SlcEntity
import android.slc.appbase.repository.remote.DataTransformUtils
import android.slc.appbase.ui.vd.wrapper.BaseListWrapper
import android.slc.appbase.ui.vd.wrapper.IBaseListWrapper
import android.slc.extras.component.tree.entity.SlcTree
import android.slc.extras.component.tree.ui.TreeListMultipleVmBox
import android.slc.extras.user.entity.DeptAndUser
import android.slc.extras.user.entity.SysDept
import android.slc.extras.user.repository.remote.DeptServiceRepository
import android.slc.extras.user.repository.remote.UserServiceRepository
import androidx.databinding.Observable
import io.reactivex.rxjava3.core.ObservableSource
import io.reactivex.rxjava3.functions.Function
import java.io.Serializable

/**
 *
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2021/12/06 下午 2:42
 *
 */
class DeptUserListMultipleVmBox : TreeListMultipleVmBox<SlcTree, Serializable>(),
    IBaseListWrapper<Serializable> {

    private val baseListWrapper: BaseListWrapper<Serializable> =
        BaseListWrapper(this)

    override fun getStartRefreshOf(): Observable {
        return baseListWrapper.getStartRefreshOf()
    }

    override fun getDataListSlcObserver(): SlcObserver<List<Serializable>> {
        return baseListWrapper.dataListSlcObserver
    }

    override fun onSucceed(data: List<Serializable>) {
        dataListOf.get()?.clear()
        dataListOf.get()?.addAll(data)
        dataListOf.notifyChange()
    }

    override fun onError(errorCode: Int, errorMessage: String?) {
        onFailed(errorCode, errorMessage)
    }

    private fun getTreeId(): Long {
        return try {
            mTreeId.toLong()
        } catch (e: NumberFormatException) {
            0
        }
    }

    override fun refresh() {
        baseListWrapper.startRefreshOf.notifyChange()
        loadMoreTree()
    }

    override fun loadMoreTree() {
        super.loadMoreTree()
        DeptServiceRepository.list(getTreeId())
            .flatMap(Function<SlcEntity<List<SysDept>?>, ObservableSource<SlcEntity<DeptAndUser>>> {
                return@Function UserServiceRepository.list(getTreeId()).map { pageModelSlcEntity ->
                    val slcEntity: SlcEntity<DeptAndUser> =
                        DataTransformUtils.slcEntity2TargetRSlcEntity(pageModelSlcEntity)
                    slcEntity.data = DeptAndUser()
                    slcEntity.data.deptId = getTreeId()
                    slcEntity.data.userList = pageModelSlcEntity.data
                    slcEntity.data.nodeDeptList = it.data
                    return@map slcEntity
                }
            }).map(
            DeptServiceRepository.data2ListGroup(
                DataTransformUtils.itemTransitionTargetList(
                    getSelectTreeList(mSelectNotParentList)
                )
            )
        ).to(bindToLifecycle()).subscribe(this.dataListSlcObserver)
    }

}